package com.hjq.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hjq.common.PageResult;
import com.hjq.entity.po.Carousel;
import com.hjq.entity.vo.PageParam;
import com.hjq.mapper.CarouselMapper;
import com.hjq.service.CarouselService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class CarouselServiceImpl implements CarouselService {
    @Autowired
    private CarouselMapper carouselMapper;

    @Override
    public Integer update(Carousel carousel) {
        carousel.setUpdatedAt(LocalDateTime.now());
        return carouselMapper.update(carousel);
    }

    @Override
    public PageResult<Carousel> findByPage(PageParam pageParam) {
        if(pageParam.getPageNum()==null){
            pageParam.setPageNum(1);
        }
        if(pageParam.getPageSize()==null){
            pageParam.setPageSize(5);
        }
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
        List<Carousel> list = carouselMapper.findByPage();

        PageInfo<Carousel> pageInfo = new PageInfo<>(list);
        return new PageResult<>(pageInfo.getList(),pageInfo.getTotal());

    }

    @Override
    public Integer delete(Integer id) {
        return carouselMapper.delete(id);
    }

    @Override
    public Integer add(Carousel carousel) {
        carousel.setCreatedAt(LocalDateTime.now());
        carousel.setUpdatedAt(LocalDateTime.now());
        return carouselMapper.add(carousel);
    }
}
